//
//  KVVisitorVC.swift
//  KVBestMicroblog
//
//  Created by 李康卫 on 16/7/19.
//  Copyright © 2016年 李康卫. All rights reserved.
//

import UIKit

//声明代理协议
protocol KVVisitorViewDelegate :NSObjectProtocol {
    //声明代理方法
    func visitorViewDidSelectRegister()
    func visitorViewDidSelectlogin()
}

class KVVisitorVC: UIView {
    
    //设置代理属性
    weak var delegate: KVVisitorViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //加载View的时候穿建子view
    override init(frame: CGRect) {
        super.init(frame: frame)
        //arc4random_uniform
        self.backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)
        //取色器取色
        
        self.setupUI()
        
    }
    // MARK: - 点击事件
    func register() {
        //代理回调到控制器(调用代理方法)
        delegate?.visitorViewDidSelectRegister()
    }
    func login() {
        delegate?.visitorViewDidSelectlogin()
    }

    // MARK: - 私有方法
    ///加载子视图
    private func setupUI() {
        self.addSubview(loopView)
        self.addSubview(overLoopView)
        self.addSubview(houseView)
        self.addSubview(lableView)
        self.addSubview(registerBtn)
        self.addSubview(loginBtn)
        
        //设置子视图约束(layout)
        //改变（translate）关闭自动布局
        loopView.translatesAutoresizingMaskIntoConstraints = false
        overLoopView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        lableView.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        //loopView 的约束
        self.addConstraint(NSLayoutConstraint(item: loopView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: loopView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        //设置遮罩的限制
        self.addConstraint(NSLayoutConstraint(item: overLoopView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: loopView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: overLoopView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: loopView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))

        
        //房子的约束
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: loopView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
            self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: loopView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //messageLabel的约束
        self.addConstraint(NSLayoutConstraint(item: lableView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: lableView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: loopView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: lableView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 260))
        
        //设置resginButton的约束
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: lableView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: lableView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 34))
        //设置loginButton的约束
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: lableView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: lableView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 34))
        
        
    }
    
    //添加核心动画
    func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置animation的属性
        animation.toValue = M_PI*2
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        //图层添加动画
        loopView.layer.addAnimation(animation, forKey: nil)
    }
    ///设置访客视图的背景
    func setVisitorInfo(imageName: String,tiltle: String) {
        //隐藏小房子
        houseView.hidden = true
        //设置图片
        loopView.image = UIImage(named: imageName)
        //让遮罩移到后
        self.sendSubviewToBack(overLoopView)
        lableView.text = tiltle
        
    }
    // MARK: - 懒加载
    private lazy var loopView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    ///设置遮罩视图
    private lazy var overLoopView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var houseView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    ///label
    private lazy var lableView: UILabel = {
        
        let lable:UILabel  = UILabel(frame: CGRectZero)
        
        lable.text = "一大波妹纸等待你的关注！一大波妹纸等待你的关注！一大波妹纸等待你的关注"
        //设置Label的属性
        lable.textAlignment = NSTextAlignment.Center
        lable.numberOfLines = 0
        lable.textColor = UIColor.darkGrayColor()
        
        return lable
    }()
    ///register
    private lazy var registerBtn: UIButton = {
        let btn: UIButton = UIButton(type: UIButtonType.Custom)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //添加按钮点击事件
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    ///login登录
    private lazy var loginBtn: UIButton = {
        let btn: UIButton = UIButton(type: UIButtonType.Custom)
        btn.setTitle("登录", forState: UIControlState.Normal)
        //设置字体颜色
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //添加按钮点击事件
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()

}
